package sortmodule;

import java.util.Arrays;

/**
 * @author by 何坤
 * @Classname QuickSort
 * @Description 快排
 * @Date 2019/10/18 17:31
 */
public class QuickSort {

    public static void main(String[] args) {
        int[] ints = {6,1,9,2,78,45,91,45,26,11,15};
        sort(ints, 0, ints.length-1);
        Arrays.stream(ints).forEach(System.out::println);
    }

    private static void sort(int[] arr, int low, int high){
        if (low < high){
            int index = getIndex(arr, low, high);
            sort(arr, 0, index - 1);
            sort(arr, index + 1, high);
        }
    }

    private static int getIndex(int[] arr, int low, int high){
        int temp = arr[low];
        while(low < high){
            while(low < high && arr[high] >= temp){
                high-- ;
            }
            arr[low] = arr[high];
            while(low < high && arr[low] <= temp){
                low++;
            }
            arr[high] = arr[low];
        }
        arr[low] = temp;
        return low;
    }
}
